<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['erp:sale-product-order:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="id" />
       <el-table-column label="内部订单号" align="center" prop="internalOrderNo" />
      <el-table-column label="生产单号" align="center" prop="productionOrderNo" />
      <el-table-column label="订单日期" align="center" prop="orderDate" />
      <el-table-column label="交货日期" align="center" prop="deliveryDate" />
      <el-table-column label="订单类型" align="center" prop="orderType" />
      <el-table-column label="报价单位" align="center" prop="quotationUnit" />
      <el-table-column label="客户采购单号ID" align="center" prop="customerPurchaseOrderId" />
      <el-table-column label="客户采购单号(订单号)" align="center" prop="customerPurchaseOrderNo" />
      <el-table-column label="BOM ID" align="center" prop="bomId" />
      <el-table-column label="BOM No" align="center" prop="bomNo" />
      <el-table-column label="货号" align="center" prop="productCode" />
      <el-table-column label="品名" align="center" prop="productName" />
      <el-table-column label="品名2" align="center" prop="productName2" />
      <el-table-column label="款型编号" align="center" prop="styleCode" />
      <el-table-column label="箱型ID" align="center" prop="boxTypeId" />
      <el-table-column label="箱型名称" align="center" prop="boxTypeName" />
      <el-table-column label="材质ID" align="center" prop="materialId" />
      <el-table-column label="材质编号" align="center" prop="materialCode" />
      <el-table-column label="材质名称" align="center" prop="materialName" />
      <el-table-column label="生产材质ID" align="center" prop="productionMaterialId" />
      <el-table-column label="生产材质编号" align="center" prop="productionMaterialCode" />
      <el-table-column label="生产材质名称" align="center" prop="productionMaterialName" />
      <el-table-column label="订单长" align="center" prop="orderLength" />
      <el-table-column label="订单宽" align="center" prop="orderWidth" />
      <el-table-column label="订单高" align="center" prop="orderHeight" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="订单规格" align="center" prop="orderSpecification" />
      <el-table-column label="订单数" align="center" prop="orderQuantity" />
      <el-table-column label="备品数" align="center" prop="sparePartsQuantity" />
      <el-table-column label="生产数" align="center" prop="productionQuantity" />
      <el-table-column label="片接方式" align="center" prop="pieceAttachmentMethod" />
      <el-table-column label="是否客户特价" align="center" prop="isCustomerSpecialPrice" />
      <el-table-column label="面积报价" align="center" prop="areaQuotation" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="体积" align="center" prop="volume" />
      <el-table-column label="排产数" align="center" prop="productionPlanQuantity" />
      <el-table-column label="入库数" align="center" prop="warehouseInQuantity" />
      <el-table-column label="送货数" align="center" prop="deliveryQuantity" />
      <el-table-column label="印刷颜色" align="center" prop="printColor" />
      <el-table-column label="报价单ID" align="center" prop="quotationId" />
      <el-table-column label="报价单号" align="center" prop="quotationNo" />
      <el-table-column label="生产长" align="center" prop="productionLength" />
      <el-table-column label="生产宽" align="center" prop="productionWidth" />
      <el-table-column label="生产高" align="center" prop="productionHeight" />
      <el-table-column label="生产规格" align="center" prop="productionSpecification" />
      <el-table-column label="横切版号" align="center" prop="crossCuttingVersionNo" />
      <el-table-column label="审核" align="center" prop="isApproved" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="中止" align="center" prop="isTerminated" />
      <el-table-column label="包装数" align="center" prop="packagingQuantity" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="结单" align="center" prop="isFinalized" />
      <el-table-column label="打印" align="center" prop="isPrinted" />
      <el-table-column label="生产备注" align="center" prop="productionRemark" />
      <el-table-column label="内外径" align="center" prop="innerOuterDiameter" />
      <el-table-column label="楞型" align="center" prop="flangeType" />
      <el-table-column label="模切算料" align="center" prop="dieCuttingCalculation" />
      <el-table-column label="模板宽" align="center" prop="templateWidth" />
      <el-table-column label="模板长" align="center" prop="templateLength" />
      <el-table-column label="模数" align="center" prop="modulus" />
      <el-table-column label="外协" align="center" prop="outsourcing" />
      <el-table-column label="产品类型" align="center" prop="productType" />
      <el-table-column label="专色" align="center" prop="specialColor" />
      <el-table-column label="机台" align="center" prop="machine" />
      <el-table-column label="装钉方式" align="center" prop="nailingMethod" />
      <el-table-column label="粘合方式" align="center" prop="adhesionMethod" />
      <el-table-column label="包装方式" align="center" prop="packagingMethod" />
      <el-table-column label="附配件" align="center" prop="accessories" />
      <el-table-column label="折数" align="center" prop="foldingNumber" />
      <el-table-column label="对裱类型" align="center" prop="laminationType" />
      <el-table-column label="送货方式" align="center" prop="deliveryMethod" />
      <el-table-column label="留版" align="center" prop="leaveVersion" />
      <el-table-column label="ctp" align="center" prop="ctp" />
      <el-table-column label="新刀模" align="center" prop="newDiemold" />
      <el-table-column label="旧刀模" align="center" prop="oldDiemold" />
      <el-table-column label="表面处理" align="center" prop="surfaceTreatment" />
      <el-table-column label="生产工艺" align="center" prop="productionProcess" />
      <el-table-column label="客户PO" align="center" prop="customerPo" />
      <el-table-column label="切角" align="center" prop="cutCorners" />
      <el-table-column label="配比数" align="center" prop="proportionQuantity" />
      <el-table-column label="产品单位" align="center" prop="productUnit" />
      <el-table-column label="子件" align="center" prop="subcomponent" />
      <el-table-column label="序号" align="center" prop="serialNo" />
      <el-table-column label="项目号" align="center" prop="projectNo" />
      <el-table-column label="客户材质" align="center" prop="customerMaterial" />
      <el-table-column label="产品描述" align="center" prop="productDescription" />
      <el-table-column label="产品备注1" align="center" prop="productRemark1" />
      <el-table-column label="产品备注2" align="center" prop="productRemark2" />
      <el-table-column label="产品备注3" align="center" prop="productRemark3" />
      <el-table-column label="产品备注4" align="center" prop="productRemark4" />
      <el-table-column label="产品备注5" align="center" prop="productRemark5" />
      <el-table-column label="钉条" align="center" prop="stapleBar" />
      <el-table-column label="加分" align="center" prop="addPoints" />
      <el-table-column label="外购" align="center" prop="isExternalPurchase" />
      <el-table-column label="生产工艺" align="center" prop="productionProcess2" />
      <el-table-column label="套数" align="center" prop="sets" />
      <el-table-column label="厚度" align="center" prop="thickness" />
      <el-table-column label="单重" align="center" prop="unitWeight" />
      <el-table-column label="天地盒" align="center" prop="isHeavenBox" />
      <el-table-column label="天地盒高" align="center" prop="heavenBoxHeight" />
      <el-table-column label="天地盒长" align="center" prop="heavenBoxLength" />
      <el-table-column label="天地盒宽" align="center" prop="heavenBoxWidth" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="套件ID" align="center" prop="setId" />
      <el-table-column label="套件名" align="center" prop="setName" />
      <el-table-column label="主件ID" align="center" prop="mainItemId" />
      <el-table-column label="主件名" align="center" prop="mainItemName" />
      <el-table-column label="其他备注" align="center" prop="otherRemark" />
      <el-table-column label="尺寸/码段" align="center" prop="sizeSegment" />
      <el-table-column label="多款类型" align="center" prop="multipleModelTypes" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="印色" align="center" prop="printColor2" />
      <el-table-column label="做法" align="center" prop="process" />
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="模板宽2" align="center" prop="templateWidth2" />
      <el-table-column label="模板长2" align="center" prop="templateLength2" />
      <el-table-column label="模数2" align="center" prop="modulus2" />
      <el-table-column label="客户手工单号" align="center" prop="customerManualOrderNo" />
      <el-table-column label="总面积" align="center" prop="totalArea" />
      <el-table-column label="包装方式" align="center" prop="packagingMethod2" />
      <el-table-column label="附加费用" align="center" prop="additionalCost" />
      <el-table-column label="参数1" align="center" prop="parameter1" />
      <el-table-column label="参数2" align="center" prop="parameter2" />
      <el-table-column label="参数3" align="center" prop="parameter3" />
      <el-table-column label="参数4" align="center" prop="parameter4" />
      <el-table-column label="参数5" align="center" prop="parameter5" />
      <el-table-column label="参数6" align="center" prop="parameter6" />
      <el-table-column label="参数7" align="center" prop="parameter7" />
      <el-table-column label="参数8" align="center" prop="parameter8" />
      <el-table-column label="参数9" align="center" prop="parameter9" />
      <el-table-column label="参数10" align="center" prop="parameter10" />
      <el-table-column label="同款" align="center" prop="isSameModel" />
      <el-table-column label="拼单号" align="center" prop="orderNumber" />
      <el-table-column label="BOM序号" align="center" prop="bomSequenceNumber" />
      <el-table-column label="BOM层级" align="center" prop="bomLevel" />
      <el-table-column label="未送货数" align="center" prop="unshippedQuantity" />
      <el-table-column label="未送货数" align="center" prop="unsentQuantity" />
      <el-table-column label="总体积" align="center" prop="totalVolume" />
      <el-table-column label="印刷版号" align="center" prop="printVersionNo" />
      <el-table-column label="BOM是否重新计价" align="center" prop="bomRecalculation" />
      <el-table-column label="材质类型ID" align="center" prop="materialTypeId" />
      <el-table-column label="材质类型" align="center" prop="materialType" />
      <el-table-column label="制版完成" align="center" prop="completionOfVersion" />
      <el-table-column label="加损耗" align="center" prop="addLoss" />
      <el-table-column label="参数11" align="center" prop="parameter11" />
      <el-table-column label="参数12" align="center" prop="parameter12" />
      <el-table-column label="参数13" align="center" prop="parameter13" />
      <el-table-column label="参数14" align="center" prop="parameter14" />
      <el-table-column label="参数15" align="center" prop="parameter15" />
      <el-table-column label="参数16" align="center" prop="parameter16" />
      <el-table-column label="参数17" align="center" prop="parameter17" />
      <el-table-column label="参数18" align="center" prop="parameter18" />
      <el-table-column label="参数19" align="center" prop="parameter19" />
      <el-table-column label="参数20" align="center" prop="parameter20" />
      <el-table-column label="单价附加费" align="center" prop="unitPriceAdditionalFee" />
      <el-table-column label="纸板单号" align="center" prop="paperboardOrderNo" />
      <el-table-column label="纸板单号ID" align="center" prop="paperboardOrderId" />
      <el-table-column label="微信推送" align="center" prop="wechatPush" />
      <el-table-column label="成本单价" align="center" prop="costPrice" />
      <el-table-column label="面积成本单价" align="center" prop="areaCostPrice" />
      <el-table-column label="折扣" align="center" prop="discount" />
      <el-table-column label="毛板重量" align="center" prop="grossBoardWeight" />
      <el-table-column label="用库存" align="center" prop="useInventory" />
      <el-table-column label="备品百分比" align="center" prop="sparePartsPercentage" />
      <el-table-column label="利润率" align="center" prop="profitMargin" />
      <el-table-column label="优惠" align="center" prop="discountPrice" />
      <el-table-column label="面积单价" align="center" prop="areaUnitPrice" />
      <el-table-column label="总重" align="center" prop="totalWeight" />
      <el-table-column label="单价加价" align="center" prop="unitPriceMarkup" />
      <el-table-column label="自定义克重" align="center" prop="customWeight" />
      <el-table-column label="标记1" align="center" prop="tag1" />
      <el-table-column label="标记2" align="center" prop="tag2" />
      <el-table-column label="标记3" align="center" prop="tag3" />
      <el-table-column label="标记4" align="center" prop="tag4" />
      <el-table-column label="标记5" align="center" prop="tag5" />
      <el-table-column label="送货地址" align="center" prop="deliveryAddress" />
      <el-table-column label="送货区域" align="center" prop="deliveryRegion" />
      <el-table-column label="件数" align="center" prop="quantity" />
      <el-table-column label="附加成本单价" align="center" prop="additionalCostUnitPrice" />
      <el-table-column label="原料成本占比" align="center" prop="rawMaterialCostRatio" />
      <el-table-column label="平方差价" align="center" prop="squarePriceDifference" />
      <el-table-column label="整版" align="center" prop="fullPage" />
      <el-table-column label="散版" align="center" prop="splitPage" />
      <el-table-column label="旧版" align="center" prop="oldVersion" />
      <el-table-column label="旧改" align="center" prop="oldModified" />
      <el-table-column label="新版" align="center" prop="newVersion" />
      <el-table-column label="平轧" align="center" prop="flatRolling" />
      <el-table-column label="圆轧" align="center" prop="roundRolling" />
      <el-table-column label="旧模" align="center" prop="oldDieCutting" />
      <el-table-column label="旧模改" align="center" prop="oldDieCuttingModified" />
      <el-table-column label="新模" align="center" prop="newDieCutting" />
      <el-table-column label="选择1" align="center" prop="choice1" />
      <el-table-column label="选择2" align="center" prop="choice2" />
      <el-table-column label="选择3" align="center" prop="choice3" />
      <el-table-column label="选择4" align="center" prop="choice4" />
      <el-table-column label="不上传文件" align="center" prop="noUploadFile" />
      <el-table-column label="倒摊面积单价" align="center" prop="downSpreadAreaUnitPrice" />
      <el-table-column label="压线" align="center" prop="pressLine" />
      <el-table-column label="纵压压线" align="center" prop="verticalPressLine" />
      <el-table-column label="财务审核" align="center" prop="financialApproval" />
      <el-table-column
        label="财务审核时间"
        align="center"
        prop="financialApprovalTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="财务审核人" align="center" prop="financialApprover" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:sale-product-order:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:sale-product-order:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <SaleProductOrderDetailForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { SaleProductOrderApi } from '@/api/erp/saleproductorder'
import SaleProductOrderDetailForm from './SaleProductOrderDetailForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  internalOrderId?: number // 内部订单id（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  internalOrderId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.internalOrderId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.internalOrderId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SaleProductOrderApi.getSaleProductOrderDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.internalOrderId) {
    message.error('请选择一个ERP销售管理-生产单')
    return
  }
  formRef.value.open(type, id, props.internalOrderId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SaleProductOrderApi.deleteSaleProductOrderDetail(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>
